@		start of assembly code
.global	_start
_start:

main:
    MOV		R0,#0   @ initialize index veriable
    MOV		R8,#4   @ load n parameter to r8, n should larger than or equal to 2

wirteloop:
    LDR		R1, =a  @ load address of a to r4
    @		write 0, which is the last element in Fibonacci sequence
    MOV		R3, #0  @ load 0 to r3, 0 is the last element in Fibonacci sequence
    LSL		R2, R0, #2   @ left shift 2 bits equivalent to multiply index*4 to get array offset
    ADD		R2, R1, R2   @ r2 now has the element address
    STR		R3, [R2]    @ write R3 to a[i]
    ADD		R0, R0, #1  @ increment index
    @		write 1, which is the second last element in Fibonacci sequence
    MOV		R3, #1  @ load 1 to r3, 1 is the second last element in Fibonacci sequence
    LSL		R2, R0, #2   @ left shift 2 bits equivalent to multiply index*4 to get array offset
    ADD		R2, R1, R2   @ r2 now has the element address
    STR		R3, [R2]    @ write R3 to a[i]
    ADD		R0, R0, #1  @ increment index

    MOV		R3, #2  @ load r3 with 2, --> i = 2


@ f[3]
@ f[0 + 3]
@ f[0] is base address, offset 3 * 4 = 12
@ 0 + 12
@ LDR R1, =a  @ load address of a to r4
@ MOV R2, #3
@ LSL R2, R2, #2 @ 3 *4
@ ADD R1, R1, R2 @ base address + offset
@ LDR R7, [R1] @ fetch f[3] to R7


fill_fibonacci_seq:
    CMP		R3, R8  @ compare,  i == n?
    BGT		return_last_element @ start to return_last_element
    @		load value of f[i - 1] to r5
    SUB		R6, R0, #1  @ decrement index by 1
    LSL		R2, R6, #2   @ left shift 2 bits equivalent to multiply index*4 to get array offset
    ADD		R2, R1, R2   @ r2 now has the element address
    LDR		R5, [R2]    @ load f[i - 1] to r5
    @		load value of f[i - 2] to r4
    SUB		R6, R0, #2  @ decrement index by 2
    LSL		R2, R6, #2   @ left shift 2 bits equivalent to multiply index*4 to get array offset
    ADD		R2, R1, R2   @ r2 now has the element address
    LDR		R4, [R2]    @ load f[i - 2] to r4
    @		r7 = f[i - 1] + f[i - 2]
    ADD		R7, R4, R5
    LSL		R2, R0, #2   @ left shift 2 bits equivalent to multiply index*4 to get array offset
    ADD		R2, R1, R2   @ r2 now has the element address
    STR		R7, [R2]    @ write R7 to a[i]
    ADD		R0, R0, #1  @ increment index
    ADD     R3, R3, #1  @ i++
    B fill_fibonacci_seq

return_last_element:
    MOV		R0,R7
END:
    B END


.data
@		set 4 byte alignment
@a[20]
.balign	4
a:		.skip 20 @ because array has 5 elements, 4*5 = 20